package edu.nwpu.zya.coreops.modules.alert.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import edu.nwpu.zya.coreops.common.utils.ShiroUtils;
import edu.nwpu.zya.coreops.modules.alert.entity.RuleEntity;
import edu.nwpu.zya.coreops.modules.alert.vo.RuleVo;
import edu.nwpu.zya.coreops.modules.monitor.entity.MetricEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import edu.nwpu.zya.coreops.modules.alert.service.RuleService;
import edu.nwpu.zya.coreops.common.utils.PageUtils;
import edu.nwpu.zya.coreops.common.utils.R;



/**
 * 
 *
 * @author zhouyuang
 * @email 742025491@qq.com
 * @date 2022-04-19 20:16:07
 */
@RestController
@RequestMapping("alert/rule")
public class RuleController {
    @Autowired
    private RuleService ruleService;

    /**
     * 根据告警类型获得可以关联的实例
     * @param type
     * @return
     */
    @RequestMapping("/instance")
    public R instance(@RequestParam("type")Integer type){
        Long userId = ShiroUtils.getUserId();
        List<String> instanceIds = ruleService.getRelatedInstancesByType(type, userId);
        return R.ok().put("instanceIds",instanceIds) ;
    }

    /**
     * 获取实例下的所有告警规则
     * @param instanceId 实例id
     * @return
     */
    @RequestMapping("/instance/rules")
    public R instanceRule(@RequestParam("type")Integer type,@RequestParam("instanceId")String instanceId){
        List<RuleEntity> rules=ruleService.getRulesByTypeAndInstance(type,instanceId);
        return R.ok().put("rules",rules);
    }

    /**
     * 根据实例id和实例类别 获取可以监控的指标
     * @param instanceId
     * @param type
     * @return
     */
    @RequestMapping("/metric")
    public R metric(@RequestParam("instanceId")String instanceId,@RequestParam("type")Integer type){
        List<MetricEntity> metrics=ruleService.getMetricsByInstanceAndType(instanceId,type);
        return R.ok().put("metrics",metrics);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("alert:rule:list")
    public R list(@RequestParam Map<String, Object> params){
        Long userId = ShiroUtils.getUserId();
        params.put("userId",userId);
        PageUtils page = ruleService.queryRuleVoPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("alert:rule:info")
    public R info(@PathVariable("id") Long id){
		RuleVo rule = ruleService.getRuleVoById(id);

        return R.ok().put("ruleVo", rule);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("alert:rule:save")
    public R save(@RequestBody RuleVo ruleVo){
		ruleService.saveRuleVo(ruleVo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("alert:rule:update")
    public R update(@RequestBody RuleVo ruleVo){
		ruleService.updateByRuleVo(ruleVo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("alert:rule:delete")
    public R delete(@RequestBody RuleVo[] ruleVos){
		ruleService.removeVoByRuleVos(Arrays.asList(ruleVos));

        return R.ok();
    }

}
